﻿using LBCMS.EntityFrameWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Data.Entity;
using LBCMS.Migrations;
using LBCMS.EntityFrameWork.FrameModel;
using LBCMS.BLL;

namespace LBCMS
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            ModelBinders.Binders.Add(typeof(BLL.PredicateBuilder.queryCondition), new BLL.PredicateBuilder.QueryModelBinder()); 

            //创建数据库
            //实例化一个数据上下文对象

            using (BLLContext dbcontext = new BLLContext())
            {
                dbcontext.Database.CreateIfNotExists();


                //如果模型更改，重新创建数据库  
                //System.Data.Entity.Database.SetInitializer(new DataInit());
                Database.SetInitializer(new MigrateDatabaseToLatestVersion<BLLContext, Configuration>());



                //初始化通用字典
                Dictionary<string, List<B_CodeValue>> allCode = new Dictionary<string, List<B_CodeValue>>();
                //先全部查询出来
                var Codes = new EntityComm().GetList<B_Code>(p => 1 == 1).ToList();
                var CodeItems = new EntityComm().GetList<B_CodeValue>(p => 1 == 1).ToList();
                foreach (var item in Codes)
                {
                    allCode.Add(item.codeName, CodeItems.Where(p => p.codeID == item.id).ToList());
                }

                Application["Codes"] = allCode;
            }

        }

        protected void Session_Start(Object sender, EventArgs e)
        {

            // 在新会话启动时运行的代码
            //如果当前
            if (Request.Cookies["LoginID"] != null)
            {
                string LoginID = Request.Cookies["LoginID"].Value;
                new BLL.login().getSessionByLoginID(LoginID);

            }



        }


    }
}
